<?php


namespace app\admin\controller\system;


use app\common\model\Apilist as ApilistModel;
use app\common\model\Apifields as ApifieldlistModel;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="接口管理")
 */
class Apifieldlist extends AdminController
{

    protected $sort = [
        'sort' => 'desc',
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new ApifieldlistModel();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $id   = input("id");
        $type   = input("type");
        $hash = ApilistModel::where("id", $id)->value("hash");
        if (!$hash) {
            $this->error('参数不能为空');
        }

        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            //字段类型
            $datatype = $this->model->datatype;
            $count    = $this->model->where(["hash"=>$hash,"type"=>$type])->count();
            $list     = $this->model->where(["hash"=>$hash,"type"=>$type])->order($this->sort)->select();
            foreach ($list as $k => $v) {
                $list[$k]["datatype"] = $datatype[$v["datatype"]];
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->setDataBrage('id', $id);
        $this->setDataBrage('type', $type);
        $this->setDataBrage('hash', $hash);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        //字段类型
        $datatype = $this->model->datatype;
        $hash     = input("hash");
        $type     = input("type");
        if (!$hash) {
            $this->error('参数不能为空');
        }
        $this->assign('hash', $hash);
        $this->assign('type', $type);
        $this->assign('datatype', $datatype);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);

            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $datatype = $this->model->datatype;
        $this->assign('datatype', $datatype);
        $this->assign('row', $row);
        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $this->checkPostRequest();
        $post = $this->request->post();
        $rule = [
            'id|ID'      => 'require',
            'field|字段' => 'require',
            'value|值'   => 'require',
        ];
        $this->validate($post, $rule);

        $row = $this->model->find($post['id']);
        empty($row) && $this->error('数据不存在');
        try {
            $row->save([
                $post['field'] => $post['value'],
            ]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }

    

}
